package com.ruoyi.common.utils.lianlu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.entity.BO.SmsSendBO;
import com.ruoyi.common.utils.lianlu.common.Constants;
import com.ruoyi.common.utils.lianlu.common.Encoder;
import com.ruoyi.common.utils.lianlu.common.Request;
import com.ruoyi.common.utils.lianlu.common.SystemClock;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class LianluSmsUtils {


    private static final String TYPE1 = "1";
    private static final HashMap<String, String> notifyAuth = new HashMap<>();
    private static final HashMap<String, String> saleAuth = new HashMap<>();
    private static final String TYPE2 = "2";
    private static final String TYPE3 = "3";

    private static final String mchId = "1020565";
    public static String Version = "1.1.0";


    static {
        notifyAuth.put("AppId", "10011676365351588");
        notifyAuth.put("AppKey", "307da7dfbfff4f61bbb16b3e49bf9014");
        saleAuth.put("AppId", "10021676365351715");
        saleAuth.put("AppKey", "d1af48ab64b9406881dfc14df922cf2b");
    }

    public static JSONObject querySmsBalance(boolean isNotify) throws Exception {
        String url = Constants.DOMAIN_API.concat("sms/product/").concat("balance");
        Map<String, Object> requestData = new HashMap<>();
        requestData.put("MchId", mchId);
        requestData.put("AppId", isNotify ? notifyAuth.get("AppId") : saleAuth.get("AppId"));
        requestData.put("Version", Constants.Version);
        requestData.put("SignType", Constants.MD5);
        requestData.put("TimeStamp", String.valueOf(SystemClock.now()));
        requestData.put("Type", TYPE3);
        requestData.put("Signature", Encoder.generateSignature(requestData, isNotify ? notifyAuth.get("AppKey") : saleAuth.get("AppKey"), Constants.MD5));
        return Request.requestOnce(url, JSON.toJSONString(requestData), true, 10000, 10000);
    }

    public JSONObject querySmsTemplateList(boolean isNotify) throws Exception {
        String url = Constants.DOMAIN_API.concat("sms/product/").concat("template/get");
        Map<String, Object> requestData = new HashMap<>();
        requestData.put("MchId", mchId);
        requestData.put("AppId", isNotify ? notifyAuth.get("AppId") : saleAuth.get("AppId"));
        requestData.put("Version", Constants.Version);
        requestData.put("SignType", Constants.MD5);
        requestData.put("TimeStamp", String.valueOf(SystemClock.now()));
        requestData.put("Type", TYPE3);
        requestData.put("Signature", Encoder.generateSignature(requestData, isNotify ? notifyAuth.get("AppKey") : saleAuth.get("AppKey"), Constants.MD5));
        return Request.requestOnce(url, JSON.toJSONString(requestData), true, 10000, 10000);
    }

    public static JSONObject templateSend(SmsSendBO sendBO) throws Exception {
        String url = Constants.DOMAIN_API.concat("sms/trade/").concat("template/send");
        Map<String, Object> requestData = new HashMap<>();
        requestData.put("MchId", mchId);
        requestData.put("AppId", sendBO.isNotify() ? notifyAuth.get("AppId") : saleAuth.get("AppId"));
        requestData.put("Version", Constants.Version);
        requestData.put("SignType", Constants.MD5);
        requestData.put("TimeStamp", String.valueOf(SystemClock.now()));
        requestData.put("TemplateId", sendBO.getTemplateCode());
        requestData.put("PhoneNumberSet", sendBO.getPhoneNumberArr());
        requestData.put("TemplateParamSet", sendBO.getTemplateParamArr());
        requestData.put("Type", TYPE3);
        requestData.put("Signature", Encoder.generateSignature(requestData, sendBO.isNotify() ? notifyAuth.get("AppKey") : saleAuth.get("AppKey"), Constants.MD5));
        return Request.requestOnce(url, JSON.toJSONString(requestData), true, 10000, 10000);
    }

}
